package com.china08.yxyapi.api.manage.edu.platform.news;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.cache.YxCacheUtil;
import com.china08.yxyapi.entity.mg.platform.YxyHomeNews;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.YxyHomeNewsRepository;

import io.swagger.annotations.ApiOperation;

/**
 * 教育局平台办公 新闻管理 发布 或者下线
 * 
 */
@CrossOrigin
@RestController
public class NewsPublishDownController {

    @Autowired
    private YxyHomeNewsRepository yhnr;

    @ApiOperation(value = "教育局上架咨询或下架咨询")
    @RequestMapping(value = "/api_yw/manage/edu/platform/news/publishordown", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(@RequestBody ReqModel reqModel) {
        String id = reqModel.id;
        Assert.isTrue(StringUtils.isNotBlank(id), "编号不能为空");
        Integer status = reqModel.status;
        Assert.isTrue(status == 1 || status == 2, "非法状态");
        RespModel model = new RespModel();
        YxyHomeNews homeNews = yhnr.findById(id);
        if (homeNews != null) {
            homeNews.setStatus(status);
            yhnr.save(homeNews);
        }
        model.setMessage("成功");
        YxCacheUtil.eviceCacheAll();
        return model;
    }

    protected static class RespModel extends YxyApiRespModel {

    }

    protected static class ReqModel extends YxyApiReqModel {
        public String id;
        // 1 发布 2 下线
        public Integer status;

        @Override
        public void autoCheck() {
            Assert.notNull(StringUtils.defaultIfBlank(id, null), "请指定咨询");
            Assert.isTrue(status == 1 || status == 2, "发生错误，请刷新页面");
        }
    }
}